package com.enation.app.web.core.action.backend.api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.enation.app.web.core.service.INavigationManager;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import com.enation.framework.util.JsonUtil;

/**
 * 
 * @function 后台导航API
 * @author 闯哥
 * @time 2016年11月28日 下午10:19:11
 */
@Controller
@RequestMapping("/core/admin/navigation")
public class NavigationAPI {

	@Autowired
	private INavigationManager navigationManager;

	/**
	 * 
	 * @function 后台保存导航栏设置
	 * @return :消息体
	 */
	@ResponseBody
	@RequestMapping(value = "/save")
	public JsonResult save(String[] title, String[] url) {

		// 检验数据
		for (String string : title) {
			if (string == null || string.equals("")) {
				return JsonResultUtil.getErrorJson("填写不能为空！");
			}
		}

		for (String string : url) {
			if (string == null || string.equals("")) {
				return JsonResultUtil.getErrorJson("填写不能为空！");
			}
		}

		if (title.length != url.length) {
			return JsonResultUtil.getErrorJson("请求错误，请重试！");
		}

		// 构造map
		List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < title.length; i++) {

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("title", title[i]);
			map.put("url", url[i]);
			params.add(map);

		}

		try {
			// 保存修改
			this.navigationManager.updateParamByid("navigation.html",
					JsonUtil.ListToJson(params));

			return JsonResultUtil.getSuccessJson("修改成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResultUtil.getErrorJson("请求错误，请重试！");
		}

	}

}
